package com.fesine.crm.controller;

import com.fesine.commons.entity.Result;
import com.fesine.commons.enums.ResultEnum;
import com.fesine.commons.util.ResultUtils;
import com.fesine.crm.exception.CrmException;
import com.fesine.crm.po.NameComparePo;
import com.fesine.crm.service.NameCompareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description: 类描述
 * @author: Fesine
 * @createTime:2017/10/11 21:21
 * @update:修改内容
 * @author: Fesine
 * @updateTime:2017/10/11 21:21
 */
@RestController
@RequestMapping("/v1")
public class NameCompareController {

    @Autowired
    private NameCompareService service;


    /**
     * 获取员工信息
     *
     * @param id
     * @return
     */
    @GetMapping("/name/{id}")
    public Result get(@PathVariable("id") Integer id) {
        NameComparePo po = new NameComparePo();
        po.setId(id);
        po = service.get(po);
        if (po != null) {
            return ResultUtils.success(po);
        }
        throw new CrmException(ResultEnum.NOT_FOUND);
    }

    /**
     * 获取员工信息列表
     *
     * @param po
     * @return
     */
    @GetMapping("/names")
    public Result list(NameComparePo po) {
        List<NameComparePo> list = service.listAll(po);
        return ResultUtils.success(list);
    }

    /**
     * 添加员工信息
     * @param po
     * @return
     */
    @PostMapping("/name")
    public Result save(NameComparePo po) {
        int i = service.save(po);
        if (i == 1) {
            return ResultUtils.success(ResultEnum.CREATED,po);
        }
        throw new CrmException(ResultEnum.INVALID_REQUEST);
    }

    /**
     * 修改员工信息
     * @param po
     * @return
     */
    @PutMapping("/name/{id}")
    public Result update(@PathVariable("id") Integer id,NameComparePo po) {
        int i = service.update(po);
        if (i == 1) {
            return ResultUtils.success(ResultEnum.CREATED,po);
        }
        throw new CrmException(ResultEnum.INVALID_REQUEST);
    }


    /**
     * 删除员工信息
     * @param id
     * @return
     */
    @DeleteMapping("/name/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        NameComparePo po = new NameComparePo();
        po.setId(id);
        int i = service.delete(po);
        if (i == 1) {
            return ResultUtils.success(ResultEnum.DELETED,id);
        }
        throw new CrmException(ResultEnum.INVALID_REQUEST);
    }

    @DeleteMapping("/names/{ids}")
    public Result deleteInfos(@PathVariable String ids) {
        String[] idsArr = ids.split(",");
        NameComparePo po;
        try {
            for (String s : idsArr) {
                po = new NameComparePo();
                po.setId(Integer.parseInt(s));
                service.delete(po);
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
            throw new CrmException(ResultEnum.INVALID_REQUEST);
        }
        return ResultUtils.success(ResultEnum.DELETED, ids);
    }

}
